﻿<%@ WebHandler Language="C#" Class="testHandler" %>

using System;
using System.Web;
using System.Linq;
using System.Xml.Linq;
using System.Xml;
using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Data;

using Cisoft.Cisoft3G.Model;
using Cisoft.Cisoft3G.BLL;


public class testHandler : IHttpHandler
{

    public static XElement tvContactsGroup;

    public void ProcessRequest(HttpContext context)
    {
        tvContactsGroup = new XElement("ul", new XAttribute("id", "ulGroup"));
        context.Response.Cache.SetNoStore();
        //context.Response.Clear();
        context.Response.ContentType = "text/html";

        XElement xmlDoc = new XElement("persons",
            new XElement("person",
                "Tom",
                new XAttribute("age", 10)),
            new XElement("person",
                "Jerry",
                new XAttribute("age", 8)));
        BindTree(0, tvContactsGroup);
        context.Response.Write(tvContactsGroup);
        

    }


    private void BindTree(int pid, XElement node)
    {
        DataTable dt = new ContactGroupBO().GetList(" CG_ParentID = " + pid + " order by CG_Order desc").Tables[0];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            DataRow dr = dt.Rows[i];
            if (int.Parse(dr["CG_ParentID"].ToString()) == 0)
            {
                XElement tn = new XElement("li", dr["CG_GroupName"].ToString(), new XAttribute("id", "_" + dr["CG_ContactGroupID"].ToString()));
                //XElement tn = new XElement("li",
                //   "<a style='text-decoration:none;' href='GetContactsHandler.ashx?ContactGroupID=" +
                //       dr["CG_ContactGroupID"].ToString() + "'>" + dr["CG_GroupName"].ToString() + "</a>",
                //   new XAttribute("id", "_" + dr["CG_ContactGroupID"].ToString()));

                node.Add(tn);
                BindTree(int.Parse(dr["CG_ContactGroupID"].ToString()), tn);
            }
            else
            {
                DataTable dt2 = new ContactGroupBO().GetList(" CG_ParentID = " + pid + " order by CG_Order desc").Tables[0];
                if (dt2.Rows.Count > 0)
                {
                    
                    List<XElement> list = new List<XElement>();
                    for (int j = 0; j < dt2.Rows.Count; j++)
                    {
                        DataRow row = dt2.Rows[j];
                        XElement tn = new XElement("li",row["CG_GroupName"].ToString(),new XAttribute("id", "_" + row["CG_ContactGroupID"].ToString()));
                        //XElement tn = new XElement("li",
                        //    "<a style='text-decoration:none;' href='GetContactsHandler.ashx?ContactGroupID=" +
                        //        row["CG_ContactGroupID"].ToString() + "'>" + row["CG_GroupName"].ToString() + "</a>",
                        //    new XAttribute("id", "_" + row["CG_ContactGroupID"].ToString()));

                        list.Add(tn);
                        BindTree(int.Parse(row["CG_ContactGroupID"].ToString()), tn);
                    }
                    XElement temp = new XElement("ul", list);
                    node.Add(temp);
                }
            }
        }
    }


    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}